﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Data.EF;
using Icoders.MarketTracker.Data.Utils;

namespace Icoders.MarketTracker.Data.Repository.MasterDataRepositories
{
    public class AuditTrailRepository : RepositoryBase, IAuditTrailRepository
    {
    private AppDataContext _ctx;

    public AuditTrailRepository(AppDataContext ctx)
       {
           _ctx = ctx;
       }

       public int Save(AuditTrail entity)
       {
           DateTime date = TimeUtil.GetDateTime();
           TblAuditTrail tbl = _ctx.TblAuditTrail.FirstOrDefault(s => s.Id == entity.Id);
           if (tbl == null)
           {
               tbl = new TblAuditTrail();
               tbl.CreatedOn = date;
               tbl.StatusId = (int)MasterEntityStatus.New;
               _ctx.TblAuditTrail.AddObject(tbl);
              
           }
           tbl.UpdatedOn = date;
           tbl.Description = entity.Description;
           tbl.UserId = entity.UserId;
           tbl.ActionTypeId = entity.ActionTypeId;

           _ctx.SaveChanges();
           return tbl.Id;
       }

       public AuditTrail GetById(int id)
       {
          var tbl = _ctx.TblAuditTrail.FirstOrDefault(s => s.Id == id);
           if (tbl == null) return null;
           return Map(tbl);
       }

       public List<AuditTrail> GetAll()
       {
           return _ctx.TblAuditTrail.ToList().Select(n => Map(n)).ToList();
       }

       public void Delete(int id)
       {
           throw new NotImplementedException();
       }

       public AuditTrail GetByActionTypeId(int actionTypeId)
       {
           var tbl = _ctx.TblAuditTrail.FirstOrDefault(s => s.ActionTypeId == actionTypeId);
           if (tbl == null) return null;
           return Map(tbl);
       }

      
       private AuditTrail Map(TblAuditTrail tblAuditTrail)
       {
           return new AuditTrail() 
           {
               Id = tblAuditTrail.Id,
               Description = tblAuditTrail.Description,
               UserId = tblAuditTrail.TblUser.Id,
               Username = tblAuditTrail.TblUser.FirstName + " " + tblAuditTrail.TblUser.LastName,
               ActionName = tblAuditTrail.ActionTypeId.ToString(),
               UpdatedOn = tblAuditTrail.UpdatedOn,
               CreatedOn = tblAuditTrail.CreatedOn,
               Status = (MasterEntityStatus)tblAuditTrail.StatusId
           };
       }

       public ValidationResultInfo Validate(AuditTrail objToValidate)
       {
           throw new NotImplementedException();
       }

        protected override string _cacheKey
        {
            get { throw new NotImplementedException(); }
        }

        protected override string _cacheListKey
        {
            get { throw new NotImplementedException(); }
        }
    }
}
